
(ns logika.logika-ns
  ;(:require )
  ;(:use )
  ;(:import )
  )



(defn attack?
  [table_vector q r]
  (let [qc (nth table_vector q) rc (nth table_vector r)]
    (or (== q r) (== qc rc) (== qc (+ rc (- r q)))
        (== qc (- rc (- r q))))))


(defn check-table?
  [table_v row]
  (not (some #(attack? table_v row %) (range row)))
  )

(defn find-solution
    "Function witch solve problem and retrun table as vector"
  [table_v row]
  (let [col (count table_v)
        nr (inc row)
        nbs (filter #(check-table? % row)
                   (map #(assoc table_v row %) (range col)))]
    (if (== nr col) nbs
    (mapcat #(find-solution % nr) nbs)
      )
    )
  )



(defn queen-recursive
  [table-size]
  (find-solution (vec (repeat table-size -1)) 0))

;algoritam and code has been taken from blog.mired.org


(defn swing-components2 [container]
  (for [i (range (.getComponentCount container))]
       (.getComponent container i)))


(defn pleace-queen [element frame counter]
    (doseq [ panel  (swing-components2 (.getContentPane frame))]
      (let [
         component (nth (swing-components2 panel) (+ (* counter 8) element)) ]
        (doto component
        (.setText (str "Q"))
        (.setCaretPosition 0))))
  )


(defn print
  [solution frame]
  (let [
    solutions (queen-recursive 8)
     vektor (vec (nth solutions solution))
        ]
      (doseq [ counter (range (count vektor))]
        (pleace-queen  (nth vektor counter) frame counter)
            )
    ))
